package com.xdf.docmanagementsystem.service.impl;

import com.xdf.docmanagementsystem.common.base.BaseService;
import com.xdf.docmanagementsystem.common.util.Example;
import com.xdf.docmanagementsystem.common.util.StringUtil;
import com.xdf.docmanagementsystem.dao.EmployeeMapper;
import com.xdf.docmanagementsystem.entity.Employee;
import com.xdf.docmanagementsystem.enums.PathEnum;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.docmanagementsystem.entity.EmpExtraFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.xdf.docmanagementsystem.dao.EmpExtraFileMapper;
import com.xdf.docmanagementsystem.service.EmpExtraFileService;
import org.springframework.util.Assert;

/**
 * @Description : com.xdf.docmanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : ZK
 * @Date : 2020-06-23 11:00:30
 */
@Service
public class EmpExtraFileServiceImpl extends BaseService implements EmpExtraFileService {
    @Autowired
    private EmpExtraFileMapper dao;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * @Describe 新增方法
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(EmpExtraFile record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(EmpExtraFile record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<EmpExtraFile> list) throws Exception {
        int count = 0;
        String savePath = new StringBuilder()
                .append(PathEnum.SAVE_PATH.getPath())
                .append(File.separator)
                .append(PathEnum.EMP_SUB_PATH.getPath())
                .append(File.separator).toString();
        for (EmpExtraFile empExtraFile : list) {
            Employee employee = employeeMapper.selectByPrimaryKey(empExtraFile.getEmployeeId());
            Assert.notNull(employee, ResponseEnum.NOT_ALLOW.getMessage());
            empExtraFile.setCreateUserId(getUserId());
            empExtraFile.setUploadTime(new Date());
            empExtraFile.setCreateTime(new Date());
            empExtraFile.setFileType(StringUtil.getFileExtension(empExtraFile.getFileName()));
            empExtraFile.setFilePath(savePath
                    + employee.getId()
                    + File.separator
                    + empExtraFile.getFileName());
            count += dao.insertSelective(empExtraFile);
        }
        return count;
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(EmpExtraFile record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(EmpExtraFile record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(EmpExtraFile record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<EmpExtraFile> list) throws Exception {
        int count = 0;
        if (list == null || list.size() <= 0) return 0;
        Integer employeeId = list.stream().filter(x -> x.getEmployeeId() != null)
                .map(x -> x.getEmployeeId())
                .findFirst().orElse(null);
        List<EmpExtraFile> dtl = dao.selectByExample(new Example().andEq("employee_id", employeeId)
                .andEq("file_status", 1));
        List<Integer> i = list.stream().filter(x -> x.getId() != null)
                .map(x -> x.getId())
                .collect(Collectors.toList());

        // 将删除的文档作废
        for (EmpExtraFile empExtraFile : dtl) {
            if (!i.contains(empExtraFile.getId())) {
                dao.updateByPrimaryKeySelective(EmpExtraFile.builder().fileStatus(0).id(empExtraFile.getId()).build());
            }
        }
        for (EmpExtraFile empExtraFile : list) {
            if (empExtraFile.getId() != null) {
                count += dao.updateByPrimaryKeySelective(EmpExtraFile.builder()
                        .id(empExtraFile.getId())
                        .fileName(empExtraFile.getFileName())
                        .remarks(empExtraFile.getRemarks()).build());
            } else {
                String savePath = new StringBuilder()
                        .append(PathEnum.SAVE_PATH.getPath())
                        .append(File.separator)
                        .append(PathEnum.EMP_SUB_PATH.getPath())
                        .append(File.separator).toString();
                empExtraFile.setUploadTime(new Date());
                empExtraFile.setCreateTime(new Date());
                empExtraFile.setFileType(StringUtil.getFileExtension(empExtraFile.getFileName()));
                empExtraFile.setFilePath(savePath
                        + employeeId
                        + File.separator
                        + empExtraFile.getFileName());
                count += dao.insertSelective(empExtraFile);
            }
        }

        return count;
    }

    /**
     * @Describe 根据主键查询
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    public EmpExtraFile selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    public List<EmpExtraFile> listQuery(EmpExtraFile record) throws Exception {
        return dao.selectByParam(record);
    }

}